// Copyright 2010 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview Shared functions.
 */

goog.provide('ads_mult.common');

goog.require('goog.array');
goog.require('goog.object');
goog.require('goog.string');

/**
 * Normalizes an Array of strings
 * @param {Array.<string>} headers Array of strings to normalize.
 * @return {Array.<string>} Normalized strings, as an Array.
 */
ads_mult.common.normalizeHeaders = function(headers) {
  var keys = goog.array.map(headers, ads_mult.common.normalizeHeader_);
  keys = goog.array.filter(keys, function(key) { return key.length > 0; });
  return keys;
};


/**
 * Normalizes a string, by removing all non-alphanumeric characters and
 * using mixed case to separate words. The output will always start
 * with a lower case letter.
 * This function is designed to produce JavaScript object property names.
 * Examples:
 * "First Name" -> "firstName"
 * "Market Cap (millions) -> "marketCapMillions
 * "1 number at the beginning is ignored" ->
 *   "numberAtTheBeginningIsIgnored"
 * @param {string} header A string to normalize.
 * @return {string} Normalized string; camelCase, no spaces, etc.
 * @private_
 */
ads_mult.common.normalizeHeader_ = function(header) {
  var key = [];
  var upperCase = false;
  for (var i = 0; i < header.length; ++i) {
    var letter = header[i];
    if (letter == ' ' && key.length > 0) {
      upperCase = true;
      continue;
    }
    if (!goog.string.isAlphaNumeric(letter)) {
      continue;
    }
    if (key.length == 0 && goog.string.isNumeric(letter)) {
      continue; // first character must be a letter
    }
    if (upperCase) {
      upperCase = false;
      key.push(letter.toUpperCase());
    } else {
      key.push(letter.toLowerCase());
    }
  }
  return key.join('');
};

/**
 * Tests to see if a cell is empty.
 * First tests to see if cell contents are strings, then tests to see
 * if contents equal ''
 * @param {string} cellData Contents of cell that we are testing.
 * @return {boolean} true if cellData's cell is empty.
 */
ads_mult.common.isCellEmpty = function(cellData) {
  return goog.string.isEmpty(cellData);
};

/**
 * Counts the number of unique values for a given column
 * @param {Array.<Object.<string,string>>} objects An Array of objects, each
 *     of which represents a row.
 * @param {string} colName The column name to count unique values for.
 * @return {number} count of unique values.
 */
ads_mult.common.countUniqueInColumn = function(objects, colName) {
  var hash = {};
  // go through each object in outputObjects and put in hash
  for (var i = 0, obj; obj = objects[i]; i++) {
    var val = obj[colName];
    hash[val] = val;
  }
  return goog.object.getCount(hash);
};

/**
 * Gets the unique values for a given column
 * @param {Array.<Object.<string,string>>} objects An Array of objects, each
 *     of which represents a row.
 * @param {string} colName The column name to get unique values for.
 * @return {Array.<string>} list of unique values.
 */
ads_mult.common.getUniqueInColumn = function(objects, colName) {
  var hash = {};
  // go through each object in outputObjects and put in hash
  for (var i = 0, obj; obj = objects[i]; i++) {
    var val = obj[colName];
    hash[val] = val;
  }
  return goog.object.getValues(hash);
};
